package com.ys.nms.module.business.controller.admin.localCompanyArea;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import com.ys.nms.framework.excel.core.util.ExcelUtils;
import com.ys.nms.module.business.controller.admin.localCompanyArea.vo.CompanyAreaListReqVO;
import com.ys.nms.module.business.controller.admin.localCompanyArea.vo.CompanyAreaRespVO;
import com.ys.nms.module.business.controller.admin.localCompanyArea.vo.CompanyAreaSaveReqVO;
import com.ys.nms.module.business.dal.dataobject.localCompanyArea.CompanyAreaDO;
import com.ys.nms.module.business.service.localCompanyArea.CompanyAreaService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.ys.nms.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 区域")
@RestController
@RequestMapping("/business/company-area")
@Validated
public class CompanyAreaController {

    @Resource
    private CompanyAreaService companyAreaService;

    @PostMapping("/create")
    @Operation(summary = "创建区域")
    @PreAuthorize("@ss.hasPermission('nms:company-area:create')")
    public CommonResult<Long> createCompanyArea(@Valid @RequestBody CompanyAreaSaveReqVO createReqVO) {
        return success(companyAreaService.createCompanyArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新区域")
    @PreAuthorize("@ss.hasPermission('nms:company-area:update')")
    public CommonResult<Boolean> updateCompanyArea(@Valid @RequestBody CompanyAreaSaveReqVO updateReqVO) {
        companyAreaService.updateCompanyArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除区域")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nms:company-area:delete')")
    public CommonResult<Boolean> deleteCompanyArea(@RequestParam("id") Long id) {
        companyAreaService.deleteCompanyArea(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得区域")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nms:company-area:query')")
    public CommonResult<CompanyAreaRespVO> getCompanyArea(@RequestParam("id") Long id) {
        CompanyAreaDO companyArea = companyAreaService.getCompanyArea(id);
        return success(BeanUtils.toBean(companyArea, CompanyAreaRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得区域列表")
    @PreAuthorize("@ss.hasPermission('nms:company-area:query')")
    public CommonResult<List<CompanyAreaRespVO>> getCompanyAreaList(@Valid CompanyAreaListReqVO listReqVO) {
        List<CompanyAreaDO> list = companyAreaService.getCompanyAreaList(listReqVO);
        return success(BeanUtils.toBean(list, CompanyAreaRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出区域 Excel")
    @PreAuthorize("@ss.hasPermission('nms:company-area:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyAreaExcel(@Valid CompanyAreaListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<CompanyAreaDO> list = companyAreaService.getCompanyAreaList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "区域.xls", "数据", CompanyAreaRespVO.class,
                        BeanUtils.toBean(list, CompanyAreaRespVO.class));
    }

}